package jmine.tec.scheduling.web.pages.task.execution;

import jmine.tec.rtm.impl.web.pages.ConsultaExceptionRecord;
import jmine.tec.scheduling.domain.ScheduledTask;
import jmine.tec.scheduling.domain.ScheduledTaskExecution;
import jmine.tec.scheduling.domain.ScheduledTaskExecutionStatus;
import jmine.tec.web.wicket.pages.Template;

import org.apache.wicket.Page;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import bancosys.tec.exception.MessageCreator;
import bancosys.tec.security.annotation.Secure;
import bancosys.tec.security.authorization.UrlPermission;
import bancosys.tec.utils.date.Timestamp;

/**
 * Tela de consulta de detalhe de uma execução.
 * 
 */
@Secure(id = "URL_SHOW_SCHEDULED_TASK", permissionType = UrlPermission.class)
public class DetalheScheduledTaskExecutionPage extends Template {

	private ScheduledTaskExecution detalhe;

	private Form<ScheduledTaskExecution> form = null;

	private Page redirect;

	/**
	 * Contrutor.
	 * 
	 * @param redirect
	 *            tela que deve ser redirecionada quando clicar no botão
	 *            "voltar".
	 * @param detalhe
	 *            o {@link ScheduledTaskExecution} tendo seus detalhes exibidos.
	 */
	public DetalheScheduledTaskExecutionPage(Page redirect,
			ScheduledTaskExecution detalhe) {
		super();
		this.initForm();
		this.detalhe = detalhe;
		this.redirect = redirect;
		this.createComponents();
	}

	/**
	 * Inicializa o formulário da página.
	 */
	private void initForm() {
		this.form = new Form<ScheduledTaskExecution>("mainForm");
		this.add(this.form);
	}

	/**
	 * Gera os componentes da página.
	 */
	private void createComponents() {
		this.form.add(new Label("id", new PropertyModel<String>(this.detalhe,
				"id")));

		this.form.add(new Label("task", new PropertyModel<ScheduledTask>(
				this.detalhe, "task")));

		this.form.add(new Label("status",
				new PropertyModel<ScheduledTaskExecutionStatus>(this.detalhe,
						"status")));

		this.form.add(new Label("start", new Model<Timestamp>(new Timestamp(
				this.detalhe.getStart()))));

		this.form.add(new Label("end", new Model<Timestamp>(new Timestamp(
				this.detalhe.getEnd()))));

		TextArea<String> txtStackTrace = new TextArea<String>("trace",
				new PropertyModel<String>(this.detalhe, "trace"));
		this.form.add(txtStackTrace);

		this.form.add(new Link<Object>("exception") {
			@Override
			public void onClick() {
				this.setResponsePage(new ConsultaExceptionRecord()
						.createViewPage(DetalheScheduledTaskExecutionPage.this.detalhe
								.getException().getId()));
			}

		}.setVisible(this.detalhe.getException() != null));

		this.form.add(new Link<Object>("btnVoltar") {
			@Override
			public void onClick() {
				this.setResponsePage(DetalheScheduledTaskExecutionPage.this.redirect);
			}
		});

	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected MessageCreator getHelpTextCreator() {
		return null;
	}
}